import { mapMutations } from 'vuex';

import { userLogin } from '@/api/user.js';
import { checkUserName ,checkPassword } from '@/utils/validator.js'

export default {
	data() {
		return {
			formData: {
				loginName: 'liqiwei123@outlook.com',
				password: 'd23s3aaf',
			}
		};
	},
	onLoad() {
		//this.formData.loginName = uni.getStorageSync('loginName') || '';
	},
	methods: {
		...mapMutations(['login']),
		async toLogin() {
			
			//校验数据
			if(!checkUserName(this.formData.loginName) || !checkPassword(this.formData.password)){
				return;
			}
			
			userLogin(this.formData).then(response => {
				let data = response.data;
				console.log(data);
				uni.setStorageSync('user',data);
				uni.setStorageSync('token',data.accessToken);
				uni.setStorageSync('tokenTime',new Date().getTime());
				uni.setStorageSync('refreshToken',data.refreshToken);
				uni.setStorageSync('loginName',this.formData.loginName);
				//状态管理
				this.login(data);
				uni.navigateBack();
			});
		},
		
		toRegist() {
			uni.navigateTo({url:'/pages/user/register/register'});
		},
	},
}